import get from '@dfeidao/atom-web/local/get';
import remove from '@dfeidao/atom-web/local/remove';
import set from '@dfeidao/atom-web/local/set';
import nodejs from '@dfeidao/atom-web/msg/nodejs';
import s_put from '@dfeidao/atom-web/storage/put';
import { spaceid } from './config';

import render from '@dfeidao/atom-web/render/render';
import { IFeidaoAiBrowserComponent } from '@dfeidao/web/interfaces';


import P00 from './P00';
import P01 from './P01';
import P02 from './P02';
import P04 from './P04';
import P05 from './P05';
import P06 from './P06';
import P07 from './P07';

interface Users {
	productid: string;
	page_no: number;
	page_size: number;
	dataArray: object[];
	count: number;
}
interface Data {

	data: string;
	cdata: Array<{
		p_id: Array<{}>;
	}>;
}

export default async function a007(fd: IFeidaoAiBrowserComponent) {
	// b init
	// const user = await whoami();
	const productid = fd.data.node.querySelector<HTMLSpanElement>('[data-feidao-id="user"]').getAttribute('prid');
	// const user = await whoami(get_user_info);
	// console.log(productid);
	await s_put('productid', productid);
	if (get(fd, 'curr_dept')) {
		remove(fd, 'curr_dept');
	}

	const res = await nodejs<{ dataArray: object[] }>('menu/zj-000001/s005', { productid: spaceid });
	render(fd, res, P07, 'P07', 'inner');
	const asd = fd.data.node.querySelector<HTMLSelectElement>('#product_clent_type');
	const client_point_id = asd.value;
	const client_style = asd.selectedOptions[0].innerText;
	const page_size = 20;
	set(fd, 'client_style', client_style);
	set(fd, 'client_point_id', client_point_id);
	set(fd, 'productid', productid);
	set(fd, 'page_size', page_size);
	set(fd, 'page_no', 1);
	const res1 = await nodejs<Users>('menu/zj-000001/s006', {
		productid, page_no: 1, page_size, client_type: client_point_id
	});
	if (res1) {
		if (res1.dataArray.length > 0) {
			set(fd, 'flag', 1);
			render(fd, res1.dataArray, P06, 'P06', 'inner');
			// console.log(res1.dataArray);
			res1.page_no = 1;
			res1.page_size = page_size;
			if (res1.count > page_size) {
				render(fd, res1, P05, 'P05', 'inner');
			}
			fd.data.node.querySelector('[data-feidao-presentation="P04"]').innerHTML = '';
		} else {
			set(fd, 'flag', 0);
			render(fd, '', P06, 'P06', 'inner');
			fd.data.node.querySelector('[data-feidao-presentation="P05"]').innerHTML = '';
			render(fd, '', P04, 'P04', 'inner');
		}
		const res2 = await nodejs<Data[]>('menu/zj-000001/s007', { productid, client_type: client_point_id });
		// console.log(res2, '000');
		if (res2.length === 0) {
			render(fd, '', P00, 'P00', 'inner');

		} else {
			render(fd, JSON.stringify(res2), P01, 'P01', 'inner');
			render(fd, JSON.stringify(res2), P02, 'P02', 'inner');
		}
	}
}
